package com.poisonh.unifiedauth.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.utils.CommonUtils;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.PreferencesUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;

/**
 * 找回密码
 * Created by PoisonH on 2016/7/14.
 */
public class FindPassWd extends BaseActivity implements View.OnClickListener
{
    //手机号码输入框
    private EditText mFindPincodeInputPhone;
    //验证码输入框
    private EditText mFindPincodeInputPhoneCode;
    //发送验证码按钮
    private Button mBtnSendCode;
    //确认按钮
    private Button mBtnSure;
    //手机号
    private String mStrPhone;
    //验证码
    private String mStrSmsCode;
    //验证码是否输入了6位
    private boolean code6bit = false;
    //手机号是否输入了11位
    private boolean phone11bit = true;

    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_findpasswd);
        initTitleView();
        initView();
    }

    @Override
    protected void initView()
    {
        setTitleTxt("找回密码");
        mRightBtn.setVisibility(View.GONE);
        mLeftBtn.setOnClickListener(this);
        mFindPincodeInputPhone = (EditText) findViewById(R.id.findpincode_input_phone);
        mFindPincodeInputPhoneCode = (EditText) findViewById(R.id.findpincode_input_phonecode);
        mBtnSendCode = (Button) findViewById(R.id.findpincode_send_phonecode);
        mBtnSure = (Button) findViewById(R.id.findpincode_sure);
        mBtnSendCode.setOnClickListener(this);
        mBtnSure.setOnClickListener(this);
        if (mStrUserPhone != null)
        {
            mFindPincodeInputPhone.setText(mStrUserPhone);
        } else
        {
            mFindPincodeInputPhone.setText(PreferencesUtils.getString(this, "PHONE"));
        }
        mFindPincodeInputPhoneCode.addTextChangedListener(mEditTextCodeWatcher);
        mFindPincodeInputPhone.addTextChangedListener(mEditTextPhoneWatcher);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;
            case R.id.findpincode_send_phonecode:
                mStrPhone = mFindPincodeInputPhone.getText().toString();
                if (mStrPhone.trim().length() != 11)
                {
                    ToastUtils.showToast(FindPassWd.this, getString(R.string.sure_input_11_phone));
                } else
                {
                    sendSmsCode(mStrPhone);
                    mBtnSendCode.setEnabled(false);
                }
                break;
            case R.id.findpincode_sure:
                mStrSmsCode = mFindPincodeInputPhoneCode.getText().toString();
                if (mStrSmsCode.length() < 6 || mStrSmsCode == null)
                {
                    ToastUtils.showToast(FindPassWd.this, "请输入6位验证码");
                } else
                {
                    HttpParams params = new HttpParams();
                    params.put("phone", mStrPhone);
                    params.put("msgCode", mStrSmsCode);
                    params.put("verifyType", "1");
                    login(params);
                }
                break;

        }
    }

    /**
     * 发送短信验证码
     */
    private void sendSmsCode(String mStrPhone)
    {
        HttpParams params = new HttpParams();
        params.put("phone", mStrPhone);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.SEND_MESSAGE, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                CommonUtils.CountdownTime(mBtnSendCode);
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(FindPassWd.this, failReason);
                mBtnSendCode.setText(R.string.login_tips_btn_phonecode);
                mBtnSendCode.setEnabled(true);
            }
        });
    }

    /**
     * 验证
     *
     * @param params
     */
    private void login(HttpParams params)
    {
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.VERIFY_RAND_NUMCODE, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                ConstantUitls.TYPE = 2;
                Intent mIntent = new Intent(FindPassWd.this, SettingPinCodeActivity.class);
                mIntent.putExtra("PHONE", mStrPhone);
                mStrUserPhone = mStrPhone;
                startActivity(mIntent);
                finish();
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(FindPassWd.this, failReason);
                mFindPincodeInputPhoneCode.setText("");
            }
        });
    }

    TextWatcher mEditTextCodeWatcher = new TextWatcher()
    {
        private CharSequence temp;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            temp = s;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
        }

        @Override
        public void afterTextChanged(Editable s)
        {
            if (temp.length() == 6)
            {
                code6bit = true;
                changeBtnLoginState();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mFindPincodeInputPhoneCode.getWindowToken(), 0);
            } else
            {
                code6bit = false;
                changeBtnLoginState();
            }
        }
    };

    TextWatcher mEditTextPhoneWatcher = new TextWatcher()
    {
        private CharSequence temp;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            temp = s;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
        }

        @Override
        public void afterTextChanged(Editable s)
        {
            if (temp.length() == 11)
            {
                phone11bit = true;
                changeBtnLoginState();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mFindPincodeInputPhone.getWindowToken(), 0);
            } else
            {
                phone11bit = false;
                changeBtnLoginState();
            }
        }
    };

    /**
     * 根据手机号、验证码、同意协议改变登录按钮状态
     */
    private void changeBtnLoginState()
    {
        if (code6bit && phone11bit)
        {
            mBtnSure.setEnabled(true);
            mBtnSure.setTextColor(getResources().getColor(R.color.btn_seleted_true));
            mBtnSure.setBackground(getResources().getDrawable(R.drawable.shape_login_btn_normal));
        } else
        {
            mBtnSure.setEnabled(false);
            mBtnSure.setTextColor(getResources().getColor(R.color.btn_seleted_flase));
            mBtnSure.setBackground(getResources().getDrawable(R.drawable.shape_login_btn_abnormal));
        }
    }
}
